Dynamic SQL Database Credentials

Description

By default all users who use an AlphaDAO connection to the SQL database using the same credentials. These credentials are defined in the AlphaDAO Named Connections defined in the application publish profile.

For example, when the developer builds an application he may specify that a List control is based on a named connection string called 'MarketingData'. In the Publishing Profile that the developer creates before publishing the application, the developer might specify that this named connection string resolves to:

{A5API='SQLServer',A5DBVersion=2008,Server='myappserver',A5ANSINullPadWarn=Default, UserName='frank',Password='mysecretPassword',Database='Northwind'}

Every user who uses this application will now connect to the SQL database using a username of 'frank' and a password of 'mysecretpassword'. In many applications, this will be perfectly fine. However, in some applications (particularly applications in which users are required to log in to the application), users may be required to connect to the SQL database using their own credentials. These user credentials can be set easily using the setSQLCredentials() method of the context.session object:

context.session.setSQLCredentials(Username as c, Password as c)

For example:

context.session.setSQLCredentials("Dave","DavesPassword")

The typical way in which this method is used is with a redirect page. The redirect page is the page where the user is sent after they log in to the Security Framework. This page can include Xbasic to set session variables and configure settings. It is also where the SQL Credentials can be set.

The following code when added to the redirect after login page will set the SQL Credentials:

<%a5
'insert code here to look up the appropriate username and password for this user.

'Then, call the context.session.setSQLCredentials() method.
context.session.setSQLCredentials("userNameForUser","passwordForUser")

if .not. Context.Session.CallResult.Success then
    ' Error: SQL Credentials were not set.
    error_generate(Context.Session.CallResult.Text) 
end if
%>

Once you have set the username and password, you can use it when you open a connection:

'get the current user name and passowrd
dim pss as p
pss = a5w_resolveUserNamePassword(session)
dim flag as l 
'open the connection using the specified username and password (overriding the username and password defined in the connection string)
flag = cn.open(connstring, pss.userName, pss.userPassword)

See Also